var data = [{'id':'1','suppliertype':'text1','suppliername':'text2','suppliercode':'text3','referred':'text4','countries':'text5','supplierlevel':'text6','suppliersource':'text7','businesstypes':'text8','url':'text9'
    ,'subordinatebuyer':'text10','companyaffiliation':'text11','buyername':'text12','email':'text13','position':'text10','phone':'text11','mobile':'text12','wechat':'text13','expressmailaddress':'text11','contactbirthday':'text12','buyer':'text13'},
    {'id':'2','suppliertype':'text1','suppliername':'text2','suppliercode':'text3','referred':'text4','countries':'text5','supplierlevel':'text6','suppliersource':'text7','businesstypes':'text8','url':'text9'
        ,'subordinatebuyer':'text10','companyaffiliation':'text11','buyername':'text12','email':'text13','position':'text10','phone':'text11','mobile':'text12','wechat':'text13','expressmailaddress':'text11','contactbirthday':'text12','buyer':'text13'},
    {'id':'3','suppliertype':'text1','suppliername':'text2','suppliercode':'text3','referred':'text4','countries':'text5','supplierlevel':'text6','suppliersource':'text7','businesstypes':'text8','url':'text9'
        ,'subordinatebuyer':'text10','companyaffiliation':'text11','buyername':'text12','email':'text13','position':'text10','phone':'text11','mobile':'text12','wechat':'text13','expressmailaddress':'text11','contactbirthday':'text12','buyer':'text13'},
    {'id':'4','suppliertype':'text1','suppliername':'text2','suppliercode':'text3','referred':'text4','countries':'text5','supplierlevel':'text6','suppliersource':'text7','businesstypes':'text8','url':'text9'
        ,'subordinatebuyer':'text10','companyaffiliation':'text11','buyername':'text12','email':'text13','position':'text10','phone':'text11','mobile':'text12','wechat':'text13','expressmailaddress':'text11','contactbirthday':'text12','buyer':'text13'}];

layui.use([ 'layer', 'table', 'form'], function() {
    var layer = layui.layer //弹层
        ,table = layui.table //表格
        form = layui.form;

    //---供应商查询
    table.render({
        elem: '#supplierselect'
        ,data:data
        ,toolbar: '#suppliertoolbar'
        ,cols: [[
            {checkbox: true, fixed: true}
            ,{field:'suppliertype', title:'供应商类型', fixed: true}
            ,{field:'suppliername', title:'供应商名称', fixed: true,event:"suppliername"}
            ,{field:'suppliercode', title:'供应商代码', fixed: true,event:"suppliercode"}
            ,{field:'referred', title:'简称', fixed: true,event:"referred"}
            ,{field:'countries', title:'国家', fixed: true}
            ,{field:'supplierlevel', title:'供应商等级', fixed: true}
            ,{field:'suppliersource', title:'供应商来源', fixed: true}
            ,{field:'businesstypes', title:'业务类型', fixed: true}
            ,{field:'url', title:'网址', fixed: true}
            ,{field:'subordinatebuyer', title:'所属采购员', fixed: true}
            ,{fixed: 'right', width:80, align:'center', toolbar: '#supplierup', title:'操作'}
        ]]
        ,id: 'suppliertableid'
        ,height: $(document).height() - $('#supplierselect').offset().top - 50
        ,page: true
    });
    table.on('toolbar(suppliertable)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'new':
                parent.intab("views/jsp/BasicData/tpl/supplierinfo.jsp","添加供应商信息")
                break;
            case 'delet':
                var data = checkStatus.data;
                var delid = [];
                for (var i = 0;i<data.length;i++){
                    delid.push(data[i].id)
                }
                layer.msg('需要删除的数据ID为：'+ JSON.stringify(delid) + ',执行删除');
                table.reload('suppliertableid',{}); //ajax删除成功之后刷新表格
                break;
            case 'searchs':
                var supplier = $('#supplier').val();
                var suppliertype = $('#suppliertype option:selected').val();
                var buyer = $('#buyer option:selected').val();
                layer.msg("搜索"+supplier+","+suppliertype+","+buyer);
                //执行重载
                table.reload('suppliertableid', {
                    data:data ,
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        'supplier': supplier,
                        'suppliertype': suppliertype,
                        'buyer' : buyer
                    }
                }, 'data');
                break;
        };
    });
    table.on('tool(suppliertable)', function(obj){
        var data = obj.data;
        if(obj.event === 'supplierup') {
            parent.intab("views/jsp/BasicData/tpl/supplierinfo.jsp?id="+obj.data.id,"修改供应商信息")
        }else if(obj.event === 'suppliername' || obj.event === 'suppliercode' || obj.event === 'referred') {
            parent.intab("views/jsp/BasicData/tpl/viewsupplierinfo.jsp?id="+obj.data.id,"供应商信息")
        }
    });

    //---联系人查询
    table.render({
        elem: '#contactselect'
        ,data:data
        ,toolbar: '#contactquerytoolbar'
        ,cols: [[
            {checkbox: true, fixed: true}
            ,{field:'companyaffiliation', title:'所属公司', fixed: true,event:"companyaffiliation"}
            ,{field:'buyername', title:'采购员名称   ', fixed: true,event:"buyername"}
            ,{field:'email', title:'邮箱地址', fixed: true}
            ,{field:'position', title:'职务', fixed: true}
            ,{field:'phone', title:'电话', fixed: true}
            ,{field:'mobile', title:'手机', fixed: true}
            ,{field:'wechat', title:'微信', fixed: true}
            ,{field:'expressmailaddress', title:'快件地址', fixed: true}
            ,{field:'contactbirthday', title:'联系人生日', fixed: true}
            ,{field:'note', title:'备注', fixed: true}
            ,{field:'buyer', title:'采购员', fixed: true}
        ]]
        ,id: 'contacttableid'
        ,height: $(document).height() - $('#contactquerytoolbar').offset().top - 50
        ,page: true
    });
    table.on('tool(contacttable)', function(obj){
        if(obj.event === 'companyaffiliation' || obj.event === 'buyername') {
            parent.intab("views/jsp/BasicData/tpl/viewsupplierinfo.jsp?id="+obj.data.id,"供应商信息")
        }
    });
    table.on('toolbar(contacttable)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'searchs':
                var contactinfo = $('#contactinfo').val();
                var contactemail = $('#contactemail').val();
                layer.msg("搜索"+contactinfo+","+contactemail);
                //执行重载
                table.reload('contacttableid', {
                    data:data,
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        'contactinfo': contactinfo,
                        'contactemail': contactemail,
                    }
                }, 'data');
                break;
        };
    });
});